package com.mobi.Survey.uicomponents;

import java.lang.reflect.Method;
import java.util.StringTokenizer;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobi.Survey.controller.SurveyDetailPojo;
import com.mobi.Survey.utils.Helper;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * CustomDatePicker.java
 * 
 * 
 */
public class CustomDatePicker extends DatePicker
{
	public static final String DELIM = "|";
	private LinearLayout ll;
	private Context ctx;

	public CustomDatePicker(Context context, SurveyDetailPojo sdp,
			boolean editable)
	{
		super(context);
		this.ctx = context;
		int currentapiVersion = android.os.Build.VERSION.SDK_INT;
		if (currentapiVersion >= 11)
		{
			try
			{
				Method m = this.getClass().getMethod("setCalendarViewShown",
						boolean.class);
				m.invoke(this, false);
			} catch (Exception e)
			{
			}
		}
		ll = new LinearLayout(ctx);

		ll.setOrientation(LinearLayout.VERTICAL);
		ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		if (sdp.getControlText() != null && sdp.getControlText().length() > 0)
		{
			CustomTextView txt = new CustomTextView(ctx);
			txt.setCustomColor(sdp);
			txt.setText(sdp.getControlText());
			ll.addView(txt);

		}
		if (sdp.getUserValue().length() > 0)
		{
			try
			{
				StringTokenizer st = new StringTokenizer(sdp.getUserValue(),
						DELIM);

				String month = st.nextToken();
				String day = st.nextToken();
				String yr = st.nextToken();
				this.updateDate(Helper.toInt(yr),
						getMonthNumberByName(month) - 1, Helper.toInt(day));

			} catch (Exception e)
			{
				e.printStackTrace();
			}

		} else if (sdp.getDefaultValue().length() > 0)
		{
			try
			{
				StringTokenizer st = new StringTokenizer(sdp.getDefaultValue(),
						DELIM);
				String day = st.nextToken();
				String yr = st.nextToken();
				String month = st.nextToken();

				this.updateDate(Helper.toInt(yr),
						getMonthNumberByName(month) - 1, Helper.toInt(day));

			} catch (Exception e)
			{
				e.printStackTrace();
			}

		}
		if (!editable)
			this.setEnabled(false);
		ll.addView(this);
	}

	void applyFontRecursively(ViewGroup parent, SurveyDetailPojo sdp)
	{
		for (int i = 0; i < parent.getChildCount(); i++)
		{
			View child = parent.getChildAt(i);
			if (child instanceof ViewGroup)
			{
				if (null != sdp && null != sdp.getSurvey()
						&& null != sdp.getSurvey().getFontColor())
				{

					applyFontRecursively((ViewGroup) child, sdp);
				}
			} else if (child != null)
			{

				if (child instanceof TextView)
				{
					TextView textView = (TextView) child;
					textView.setTextColor(Color.parseColor("#"
							+ sdp.getSurvey().getFontColor()));

				}
			}
		}
	}

	public static final String getMonthNameByNumber(byte month)
	{
		String str_month = "";
		switch (month)
		{
		case 1:
			str_month = "Jan";
			break;

		case 2:
			str_month = "Feb";
			break;
		case 3:
			str_month = "Mar";
			break;
		case 4:
			str_month = "Apr";
			break;
		case 5:
			str_month = "May";
			break;
		case 6:
			str_month = "Jun";
			break;
		case 7:
			str_month = "Jul";
			break;
		case 8:
			str_month = "Aug";
			break;
		case 9:
			str_month = "Sep";
			break;

		case 10:
			str_month = "Oct";
			break;

		case 11:
			str_month = "Nov";
			break;

		case 12:
			str_month = "Dec";
			break;

		}

		return str_month;

	}

	private int getMonthNumberByName(String month)
	{

		if (month.equalsIgnoreCase("Jan"))
		{
			return 1;
		} else if (month.equalsIgnoreCase("Feb"))
		{
			return 2;
		} else if (month.equalsIgnoreCase("Mar"))
		{
			return 3;
		} else if (month.equalsIgnoreCase("Apr"))
		{
			return 4;
		} else if (month.equalsIgnoreCase("May"))
		{
			return 5;
		} else if (month.equalsIgnoreCase("Jun"))
		{
			return 6;
		} else if (month.equalsIgnoreCase("Jul"))
		{
			return 7;
		} else if (month.equalsIgnoreCase("Aug"))
		{
			return 8;
		} else if (month.equalsIgnoreCase("Sep"))
		{
			return 9;
		} else if (month.equalsIgnoreCase("Oct"))
		{
			return 10;
		} else if (month.equalsIgnoreCase("Nov"))
		{
			return 11;
		} else if (month.equalsIgnoreCase("Dec"))
		{
			return 12;
		}

		return 0;

	}

	public View getView()
	{
		return ll;
	}
}
